/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: XjfWorkOrderEntryServiceImpl
 * Author:   Administrator
 * Date:     2021/8/6 0006 20:45
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package cn.hxlc.tie.demo.modular.jf.service.impl;

import cn.hutool.core.bean.BeanUtil;

import cn.hxlc.tie.demo.modular.jf.entity.XjfWorkOrderEntry;
import cn.hxlc.tie.demo.modular.jf.mapper.XjfWorkOrderEntryMapper;
import cn.hxlc.tie.demo.modular.jf.model.params.XjfInStoreParam;
import cn.hxlc.tie.demo.modular.jf.model.params.XjfReportParam;
import cn.hxlc.tie.demo.modular.jf.model.params.XjfReqMaterialParam;
import cn.hxlc.tie.demo.modular.jf.model.params.XjfWorkOrderEntryParam;
import cn.hxlc.tie.demo.modular.jf.model.result.XjfWorkOrderEntryResult;
import cn.hxlc.tie.demo.modular.jf.service.XjfWorkOrderEntryService;
import cn.hxlc.tie.libs.mp.page.PageFactory;

import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2021/8/6 0006
 * @since 1.0.0
 */
@Service
@Transactional
public class XjfWorkOrderEntryServiceImpl extends ServiceImpl<XjfWorkOrderEntryMapper, XjfWorkOrderEntry> implements XjfWorkOrderEntryService {



    @Override
    public PageResult<XjfWorkOrderEntryResult> findPageBySpec(XjfWorkOrderEntryParam param){
        Page pageContext = getPageContext();
        IPage<XjfWorkOrderEntryResult> page = this.baseMapper.customPageList(pageContext, param);
        return new PageResult<>(page);
    }

    @Override
    public PageResult<XjfWorkOrderEntryResult> queryXdEntryListPage(XjfWorkOrderEntryParam param){
        Page pageContext = getPageContext();
        IPage<XjfWorkOrderEntryResult> page = this.baseMapper.queryXdEntryListPage(pageContext, param);
        return new PageResult<>(page);
    }

    @Override
    public PageResult<XjfWorkOrderEntryResult> queryInstoreEntryListPage(XjfWorkOrderEntryParam param){
        Page pageContext = getPageContext();
        IPage<XjfWorkOrderEntryResult> page = this.baseMapper.queryInstoreEntryListPage(pageContext, param);
        return new PageResult<>(page);
    }

    @Override
    public XjfWorkOrderEntry detail(Long fid) {
        return this.baseMapper.selectById(fid);
    }

    @Override
    public List<XjfWorkOrderEntryResult> queryEntry(Long fid) {
        return this.baseMapper.queryDetail(fid);
    }

    @Override
    public XjfWorkOrderEntryResult queryByFmaterialId(Long fmaterialid) {
        return this.baseMapper.queryByFmaterialId(fmaterialid);
    }

    @Override
    public List<XjfWorkOrderEntryResult> findListBySpec(XjfWorkOrderEntryParam param){

        return this.baseMapper.customList(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(XjfWorkOrderEntryParam param){
        XjfWorkOrderEntry entity = getEntity(param);
        this.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateReqMaterial(XjfReqMaterialParam param){

        Long fid = param.getFworkorderentrtyid();

        XjfWorkOrderEntry xjfWorkOrderEntry = this.baseMapper.selectById(fid);

            xjfWorkOrderEntry.setFreqqty(param.getFreqqty().add(xjfWorkOrderEntry.getFreqqty()));
            this.updateById(xjfWorkOrderEntry);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateReport(XjfReportParam param){
        Long fid = param.getFworkorderentrtyid();
        XjfWorkOrderEntry xjfWorkOrderEntry = this.baseMapper.selectById(fid);

       xjfWorkOrderEntry.setFgoodqty(param.getFgoodqty());
       xjfWorkOrderEntry.setFbadqty(param.getFbadqty());
       xjfWorkOrderEntry.setFreqqty(param.getFgoodqty().add(param.getFbadqty()));
       this.updateById(xjfWorkOrderEntry);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateInStore(XjfInStoreParam param){
        Long fid = param.getFworkorderentrtyid();
        XjfWorkOrderEntry xjfWorkOrderEntry = this.baseMapper.selectById(fid);

        xjfWorkOrderEntry.setFinstoreqty(param.getFgoodqty());
        this.updateById(xjfWorkOrderEntry);
    }

    private XjfWorkOrderEntry getEntity(XjfWorkOrderEntryParam param) {
        XjfWorkOrderEntry entity = new XjfWorkOrderEntry();
        BeanUtil.copyProperties(param, entity);
        return entity;
    }
    private Page getPageContext() {
        return PageFactory.defaultPage();
    }
}
